{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scala API to the Table Widget\n",
    "\n",
    "This API is being [improved](https://github.com/twosigma/beakerx/issues/5798)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val table = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "import java.util.concurrent.TimeUnit\n",
    "\n",
    "val display = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "//show all time columns in days\n",
    "display.setStringFormatForTimes(TimeUnit.DAYS)\n",
    "//min 4, max 6 decimal places for all doubles\n",
    "display.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(4,6))\n",
    "//setting for a column takes precidence over the type\n",
    "display.setStringFormatForColumn(\"m3\", TableDisplayStringFormat.getDecimalFormat(0, 0))\n",
    "//set the alignment\n",
    "display.setAlignmentProviderForType(ColumnType.Double, TableDisplayAlignmentProvider.RIGHT_ALIGNMENT)\n",
    "display.setAlignmentProviderForColumn(\"m3\", TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n",
    "\n",
    "display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display.setStringFormatForTimes(TimeUnit.HOURS)\n",
    "\n",
    "display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer\n",
    "import java.util.concurrent.TimeUnit\n",
    "\n",
    "val display2 = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "//right now, the only renderer option is for data bars\n",
    "display2.setRendererForType(ColumnType.Double, TableDisplayCellRenderer.getDataBarsRenderer())\n",
    "//use the false parameter to hide the String value\n",
    "display2.setRendererForColumn(\"y10\", TableDisplayCellRenderer.getDataBarsRenderer(false))\n",
    "\n",
    "display2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer\n",
    "\n",
    "val display3 = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "display3.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(9,9))\n",
    "//freeze a column\n",
    "display3.setColumnFrozen(\"y1\", true)\n",
    "//hide a column\n",
    "display3.setColumnVisible(\"y30\", false)\n",
    "\n",
    "//explicitly set column order/visiblity\n",
    "display3.setColumnOrder(List(\"m3\", \"y1\", \"y5\", \"time\", \"y2\")) //Columns in the list will be shown in the provided order. Columns not in the list will be hidden.\n",
    "display3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.table.format.TableDisplayStringFormat\n",
    "import com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer\n",
    "import com.twosigma.beakerx.table.highlight.TableDisplayCellHighlighter\n",
    "\n",
    "val display4 = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m3\", TableDisplayCellHighlighter.FULL_ROW))\n",
    "\n",
    "//the following two overloads should also be supported\n",
    "//set the min and max used for calculating the color\n",
    "//display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"y1\", TableDisplayCellHighlighter.FULL_ROW, 0, 5))\n",
    "//set the colors used for the min and max\n",
    "//display4.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m6\", TableDisplayCellHighlighter.SINGLE_COLUMN, null, null, Color.YELLOW, Color.BLUE))\n",
    "\n",
    "display4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "\n",
    "val map = Seq(Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n",
    "               Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n",
    "               Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 9))\n",
    "\n",
    "val display5 = new TableDisplay(map)\n",
    "\n",
    "display5.addCellHighlighter(new CellHighlighter {\n",
    "       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {\n",
    "            if (display.getValues.get(row).get(column).asInstanceOf[Int] > 5) Color.RED else Color.GREEN\n",
    "       }\n",
    "})\n",
    "\n",
    "display5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.chart.Color\n",
    "import com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter\n",
    "\n",
    "val display6 = new TableDisplay(new CSV().readFile(\"../resources/data/interest-rates.csv\"))\n",
    "display6.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m3\", 0, 8, Color.ORANGE, Color.PINK))\n",
    "display6.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m6\", HighlightStyle.SINGLE_COLUMN, 6, 8, Color.BLACK, Color.PINK))\n",
    "\n",
    "display6.addCellHighlighter(new ThreeColorHeatmapHighlighter(\"y1\", HighlightStyle.SINGLE_COLUMN, 4, 6, 8, new Color(247,106,106), new Color(239,218,82), new Color(100,189,122)))\n",
    "\n",
    "//wipe out all highlighting\n",
    "//display6.removeAllCellHighlighters()\n",
    "display6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.chart.Color\n",
    "import com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter\n",
    "\n",
    "val table = new TableDisplay(Seq(Seq(1,2,3), \n",
    "                                 Seq(3,4,5), \n",
    "                                 Seq(6,2,8), \n",
    "                                 Seq(6,2,8), \n",
    "                                 Seq(6,2,8), \n",
    "                                 Seq(6,4,8), \n",
    "                                 Seq(6,2,8), \n",
    "                                 Seq(6,2,8), \n",
    "                                 Seq(6,5,8)), \n",
    "                                 Seq(\"a\", \"b\", \"c\"), \n",
    "                                 Seq(\"double\", \"double\", \"double\"))\n",
    "\n",
    "table.addCellHighlighter(TableDisplayCellHighlighter.getUniqueEntriesHighlighter(\"b\", HighlightStyle.FULL_ROW))\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "\n",
    "val mapList4 = Seq(\n",
    "   Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n",
    "   Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n",
    "   Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 9)\n",
    ")\n",
    "\n",
    "val display7 = new TableDisplay(mapList4)\n",
    "\n",
    "//run tagged cell on action\n",
    "display7.addContextMenuItem(\"run misc_formatting\", \"misc_formatting\");\n",
    "display7.setDoubleClickAction(\"misc_formatting\");\n",
    "\n",
    "//add a context menu item\n",
    "display7.addCellHighlighter(new CellHighlighter {\n",
    "       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {\n",
    "            if (display.getValues.get(row).get(column).asInstanceOf[Int] > 5) Color.RED else Color.GREEN\n",
    "       }\n",
    "})\n",
    "\n",
    "display7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "\n",
    "val mapList4 = List(\n",
    "   Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n",
    "   Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n",
    "   Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 9)\n",
    ")\n",
    "\n",
    "val display7 = new TableDisplay(mapList4)\n",
    "\n",
    "//add a context menu item\n",
    "display7.addContextMenuItem(\"print\", new ContextMenuAction {\n",
    "       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Unit = {\n",
    "            print (display.getValues.get(row).get(column))\n",
    "       }\n",
    "})\n",
    "\n",
    "//run tagged cell on action\n",
    "display7.addContextMenuItem(\"run misc_formatting\", \"misc_formatting\");\n",
    "display7.setDoubleClickAction(\"misc_formatting\");\n",
    "\n",
    "display7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "misc_formatting"
    ]
   },
   "outputs": [],
   "source": [
    "\"run misc_formatting\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "import com.twosigma.beakerx.scala.fileloader.CSV\n",
    "import com.twosigma.beakerx.chart.Color\n",
    "import java.{lang, util}\n",
    "\n",
    "val mapList5 = List(\n",
    " Map(\"firstCol\" -> 1, \"secondCol\" -> 2, \"thirdCol\" -> 3),\n",
    " Map(\"firstCol\" -> 4, \"secondCol\" -> 5, \"thirdCol\" -> 6),\n",
    " Map(\"firstCol\" -> 9, \"secondCol\" -> 8, \"thirdCol\" -> 7))\n",
    "\n",
    "val td4 = new TableDisplay(mapList5)\n",
    "\n",
    "td4.setTooltip(new TooltipAction {\n",
    "    override def apply(row: Integer, col: Integer, display: com.twosigma.beakerx.table.TableDisplay): String = {\n",
    "         \"The value is: \" + display.getValues().get(row).get(col)\n",
    "    }\n",
    "})\n",
    "\n",
    "//set the font size and color\n",
    "td4.setDataFontSize(15)\n",
    "td4.setHeaderFontSize(30)\n",
    "\n",
    "val colors = Seq(\n",
    "            Seq(Color.LIGHT_GRAY, Color.GRAY, Color.RED),\n",
    "            Seq(Color.YELLOW, Color.ORANGE, Color.RED),\n",
    "            Seq(Color.MAGENTA, Color.BLUE, Color.BLACK))\n",
    "\n",
    "td4.setFontColorProvider(new FontColorProvider {\n",
    "    override def apply(row: Integer, col: Integer, display: com.twosigma.beakerx.table.TableDisplay): Color = {\n",
    "        colors(row)(col)\n",
    "    }\n",
    "})\n",
    "\n",
    "td4.setRowFilter(new RowFilter {\n",
    "override def apply(row: Integer, values: util.List[util.List[_]]): lang.Boolean = {\n",
    "        if (values.get(row).get(0).asInstanceOf[Int] > 0) true else false\n",
    "    }\n",
    "})\n",
    "\n",
    "//set vertical headers\n",
    "//you can also do this in the right-click menu\n",
    "td4.setHeadersVertical(true)\n",
    "\n",
    "td4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val abc = 0 // test variable\n",
    "val mapList = Seq(\n",
    "   Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n",
    "   Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n",
    "   Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 8)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "    \n",
    "val display1 = new TableDisplay(mapList)\n",
    "\n",
    "//add a context menu item\n",
    "display1.addContextMenuItem(\"print\", new ContextMenuAction {\n",
    "       override def apply(row: Integer, column: Integer, display: com.twosigma.beakerx.table.TableDisplay): Unit = {\n",
    "            print (display.getValues.get(row).get(column))\n",
    "       }\n",
    "})\n",
    "\n",
    "display1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.scala.table._\n",
    "    \n",
    "val display2 = new TableDisplay(mapList)\n",
    "\n",
    "//run tagged cell on action\n",
    "display2.addContextMenuItem(\"run print cell\", \"print_cell\");\n",
    "display2.setDoubleClickAction(\"print_cell\");\n",
    "\n",
    "display2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "print_cell"
    ]
   },
   "outputs": [],
   "source": [
    "println(\"run print cell\")\n",
    "println(display2.details)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Seamless update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val mapToUpdate = Seq(\n",
    "   Map(\"a\" -> 1, \"b\" -> 2, \"c\" -> 3),\n",
    "   Map(\"a\" -> 4, \"b\" -> 5, \"c\" -> 6),\n",
    "   Map(\"a\" -> 7, \"b\" -> 8, \"c\" -> 8)\n",
    ")\n",
    "val tableToUpdate = new TableDisplay(mapToUpdate)\n",
    "\n",
    "tableToUpdate\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tableToUpdate.updateCell(2,\"c\",121)\n",
    "tableToUpdate.sendModel()"
   ]
  }
 ],
 "metadata": {
  "beakerx_kernel_parameters": {},
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "",
   "name": "Scala",
   "nbconverter_exporter": "",
   "version": "2.12.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
